package com.scalar.chiptrack.sales.actions;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.dao.EndUserDAO;
import com.scalar.chiptrack.sales.forms.CustomerPartNumbersForm;
import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * Author: Prabhanjan
 * Date: Mar 18, 2004
 * Time: 11:25:46 PM
 */

public class EndUserPartNumbersAction extends ChipTrackAction {

    private static final String ACTION_NAME = "/endUserPartNumbers.do" ;


    public ActionForward execute (ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
                                     throws Exception {

        HttpSession session = request.getSession();
        HashMap errors = new HashMap();
		request.setAttribute (SessionKeys.ERROR_MESSAGES, errors);

		HashMap info = new HashMap();
		request.setAttribute (SessionKeys.INFO_MESSAGES, info);
        User user = (User)session.getAttribute (SessionKeys.USER_KEY);

        if (user == null)
        {
            session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward ("login"));
        }
        String endUserId = request.getParameter("endUserId");

        CustomerPartNumbersForm customerPartNumbersForm=(CustomerPartNumbersForm) form;
		String action = request.getParameter("action");

      	if("SaveEndUserPartNumbers".equalsIgnoreCase(action))
        {
            getAllCustomerPartNumbers(endUserId,request,customerPartNumbersForm);
            info.put("End User Part Numbers saved successfully",null); /*modified by Krishna Prasad on 20 May 2004 , just the changed the message*/
        }
        if(endUserId != null)
        {
            request.setAttribute("mfgPartsWithProductIds",EndUserDAO.loadAllMfgPartNumbersWithProductIdsForEndUser(Integer.parseInt(endUserId)));
        }

        return (mapping.findForward ("success"));
    }

    private static void getAllCustomerPartNumbers(String endUserId,HttpServletRequest request,CustomerPartNumbersForm customerPartNumbersForm) throws ScalarException
    {
        int end_user_id = 0;

        String[] productIds = request.getParameterValues("productId");
        String[] customerPartNumbers = request.getParameterValues("customerPartNo");
        String[] labelNumbers = request.getParameterValues("labelNumber");
        if(endUserId != null)
        {
            end_user_id = Integer.parseInt(endUserId);
        }
        EndUserDAO.saveEndUserPartNumbers( end_user_id, productIds, customerPartNumbers, labelNumbers );

     }

}